package com.bingxue.edu.management.system.controller;

import cn.hutool.core.lang.tree.Tree;
import com.bingxue.edu.management.system.model.query.OrgQuery;
import com.bingxue.edu.management.system.model.req.OrgReq;
import com.bingxue.edu.management.system.model.req.OrgSortReq;
import com.bingxue.edu.management.system.model.resp.OrgDetailResp;
import com.bingxue.edu.management.system.model.resp.OrgResp;
import com.bingxue.edu.management.system.service.OrgService;
import com.bingxue.edu.framework.common.model.Response;
import com.bingxue.edu.framework.common.model.SortQuery;
import com.bingxue.edu.framework.mybatis.query.QueryBuilder;
import com.bingxue.edu.framework.security.ApiResource;
import com.mybatisflex.core.query.QueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 组织接口
 *
 * @author panbo
 * @since 2024/07/09 19:52
 */
@Tag(name = "组织接口")
@Validated
@RestController
@RequestMapping("/system/org")
@RequiredArgsConstructor
public class OrgController {

    private final OrgService orgService;

    @Operation(summary = "查询组织树")
//    @ApiResource("system:org:manage")
    @GetMapping("/tree")
    public Response<List<Tree<String>>> tree(OrgQuery query, SortQuery sortQuery) {
        QueryWrapper queryWrapper = QueryBuilder.build(query, sortQuery);
        List<Tree<String>> tree = orgService.tree(queryWrapper);
        return Response.success(tree);
    }

    @Operation(summary = "查询列表")
//    @ApiResource("system:org:manage")
    @GetMapping("/list")
    public Response<List<OrgResp>> list(OrgQuery query, SortQuery sortQuery) {
        QueryWrapper queryWrapper = QueryBuilder.build(query, sortQuery);
        List<OrgResp> list = orgService.listAs(queryWrapper, OrgResp.class);
        return Response.success(list);
    }

    @Operation(summary = "查询详情")
//    @ApiResource("system:org:manage")
    @GetMapping("/detail/{id}")
    public Response<OrgDetailResp> detail(@PathVariable String id) {
        OrgDetailResp detail = orgService.getMapper().selectOneWithRelationsByIdAs(id, OrgDetailResp.class);
        return Response.success(detail);
    }

    @Operation(summary = "查询详情")
    @ApiResource("system:org:manage")
    @GetMapping("/summary/{id}")
    public Response<OrgDetailResp> summary(@PathVariable String id) {
        orgService.getSummary(id);
        OrgDetailResp detail = orgService.getMapper().selectOneWithRelationsByIdAs(id, OrgDetailResp.class);
        return Response.success(detail);
    }

    @Operation(summary="创建数据")
    @ApiResource("system:org:manage")
    @PostMapping("/create")
    public Response<Void> create(@RequestBody OrgReq req) {
       orgService.createOrg(req);
       return Response.success();
    }

    @Operation(summary="更新数据")
    @ApiResource("system:org:manage")
    @PostMapping("/update/{id}")
    public Response<Void> update(@RequestBody OrgReq req, @PathVariable String id) {
        orgService.updateOrg(req, id);
        return Response.success();
    }

    @Operation(summary="排序组织")
    @ApiResource("system:org:manage")
    @PostMapping("/sort")
    public Response<Void> sort(@RequestBody OrgSortReq req) {
        orgService.sortOrg(req);
        return Response.success();
    }


    @Operation(summary="删除数据")
    @ApiResource("system:org:manage")
    @PostMapping("/delete/{ids}")
    public Response<Void> delete(@PathVariable List<String> ids) {
        orgService.removeOrgByIds(ids);
        return Response.success();

    }

}
